// Mmanagement/admodifyFitem/admodifyFitem.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navs:['店铺基本信息','店主信息','店员信息','关联文件信息'],
    cindex:0,
    checklist:[],
    imgurl:'',
    hidden:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var shopnumber=options.shopnumber;
    that.setData({
      shopnumber
    })
    // 请求店铺基本信息
    app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:4,shopnumber}).then(res=>{
      that.setData({
        forms:res
      })
    })
  },
  navclick(e){
    var that=this;
    var cindex=e.target.dataset.index;
    this.setData({
      cindex
    })
    if(cindex==0){
      app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:4,shopnumber:that.data.shopnumber}).then(res=>{
          that.setData({
            forms:res
          })
        })
    }else if(cindex==1){
      app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:5,shopnumber:that.data.shopnumber}).then(res=>{
        that.setData({
          forms:res
        })
      })
    }else if(cindex==2){
      app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:6,shopnumber:that.data.shopnumber}).then(res=>{
        that.setData({
          forms:res
        })
      })
    }else if(cindex==3){
      app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:7,shopnumber:that.data.shopnumber,bname:'Mmfiletables'}).then(res=>{
        that.setData({
          forms:res
        })
      })
    }
  },
  bindSubmit(e){ //店铺基本信息
    var shopnumber=this.data.shopnumber;
    var shopname = e.detail.value.shopname;
    var shoptype = e.detail.value.shoptype;
    var storeaddress = e.detail.value.storeaddress;
    var storearea = e.detail.value.storearea;
    var landlord = e.detail.value.landlord;
    var ltelephone = e.detail.value.ltelephone;
    var remarks = e.detail.value.remarks;
    // 发送请求
    app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:17,shopnumber,shopname,shoptype,storeaddress,storearea,landlord,ltelephone,remarks}).then(res=>{
      console.log(res)
    })
  },
   // 店主表单
   Mmshopkeeper(e){
    var bname='Mmshopkeeper';
    var itemid=e.target.dataset.itemid;
    var sname=e.detail.value.sname;
    var stelephone=e.detail.value.stelephone;
    var teleyz=/^1[3456789]\d{9}$/;
    if(sname!=''&&stelephone!=''){
      if(teleyz.test(stelephone)){
        app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:8,bname,itemid,sname,stelephone}).then(res=>{
          wx.showToast({
            title: res,
            icon:'none'
          })
        })
      }else{
        wx.showToast({
          title: '请输入正确的号码格式',
          icon:'none'
        })
      }
    }else{
      wx.showToast({
        title: '输入框不能为空',
        icon:'none'
      })
    }
  },
  // 店员表单
  Mmclerks(e){
    var bname='Mmclerks';
    var itemid=e.target.dataset.itemid;
    var cname=e.detail.value.cname;
    var ctelephone=e.detail.value.ctelephone;
    var teleyz=/^1[3456789]\d{9}$/;
    if(cname!=''&&ctelephone!=''){
      if(teleyz.test(ctelephone)){
        app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:9,bname,itemid,cname,ctelephone}).then(res=>{
          wx.showToast({
            title: res,
            icon:'none'
          })
        })
      }else{
        wx.showToast({
          title: '请输入正确的号码格式',
          icon:'none'
        })
      }
    }else{
      wx.showToast({
        title: '输入框不能为空',
        icon:'none'
      })
    }
  },
  // 删除按钮
  delect(e){
    var bname=e.target.dataset.bname;
    var itemid=e.target.dataset.id;
    var shopnumber=this.data.shopnumber;
    app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php',{num:11,bname,itemid}).then(res=>{
        wx.showToast({
          title: res,
          icon:'none'
        })
        if(res="删除成功"){
          app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php',{num:7,bname,shopnumber}).then(res=>{
            this.setData({
              inptvalue:res
            })
          })
        }
    })
  },
  pdfclick(e){
    var pdfurl=e.target.dataset.pdfurl;
    wx.downloadFile({
      url: pdfurl,   
      success: function (res) {                           
        if (res.statusCode === 200) { 
          var Path = res.tempFilePath;
          wx.openDocument({
            fileType: 'pdf', 
            filePath: Path,         
            success: function (res) {
              console.log('打开 PDF 成功');
            }
          })
        }
      },
      fail: function (res) {
        console.log(res);  
      }
    })
  },
  imgclick(e){
    var imgurl=e.target.dataset.pdfurl;
    this.setData({
      imgurl,
      hidden:false
    })
  },
  imgclick2(){
    this.setData({
      hidden:true
    })
  },
  checkclick(e){
    var qlist=this.data.checklist;
    qlist.push(e.detail.value);
    this.setData({
      checklist:qlist
    })
  },
  formSubmit(e){
    var checklist=this.data.checklist;
    for(var i=0;i<checklist.length;i++){
      var id=checklist[i]
      app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:18,id}).then(res=>{
      // 重新请求数据
      app.comrequest('https://ipass.cc/myapi/Mmanagement/MUloginxg.php', {num:7,shopnumber:this.data.shopnumber,bname:'Mmfiletables'}).then(res=>{
        this.setData({
          forms:res
        })
      })
    })
    }
  }
})